package com.tjnu.baiyi.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tjnu.baiyi.entity.TbAlbum;
import com.tjnu.baiyi.entity.TbUser;

@Scope("prototype")
@Controller("myHomeAction")
public class MyHomeAction extends BaseAction {
	private static final long serialVersionUID = 6477788665656950510L;

	// 创建相册
	public String createAlbum() throws Exception {
		System.out.println("MyHomeAction.createAlbum()");
		if (album != null) {
			// 获取当前登录的用户：评论需要用到该用户的id
			TbUser user1 = (TbUser) session.getAttribute("currentUser");
			String id = request.getParameter("albumTypeId");
			System.out.println("id:" + id + " ,albumName:"
					+ album.getAlbumName() + " ,albumRestrict:"
					+ album.getAlbumRestrict());
			Boolean flag = tbAlbumDao.createAlbum(album.getAlbumName(),
					Integer.parseInt(id), album.getAlbumRestrict(),
					user1.getUserId());
			if (flag) {// 创建相册成功，更新我的所有相册
				// 获取我的所有相册(根据相册分类的先后进行排序)
				List<TbAlbum> albums = tbAlbumDao
						.findAllMyAlbumBytbAlbumTypeId(user1.getUserId());
				session.setAttribute("myAlbum", albums);
				return SUCCESS;
			} else {
				return ERROR;
			}
		} else {
			return ERROR;
		}
	}

	// 添加相册分类
	public String addAlbumType() throws Exception {
		if (albumType.getAlbumType() != null
				&& !albumType.getAlbumType().equals("")) {
			System.out.println("albumType" + albumType.getAlbumType());
			tbAlbumTypeDao.save(albumType);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	// 修改相册分类
	public String updateAlbumType() throws Exception {
		String id = request.getParameter("id").toString();
		if (albumType.getAlbumType() != null
				&& !albumType.getAlbumType().equals("")) {
			System.out.println("update albumType" + albumType.getAlbumType());
			albumType.setAlbumTypeId(Integer.parseInt(id));
			tbAlbumTypeDao.merge(albumType);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	// 删除相册分类
	public String deleteAlbumType() throws Exception {
		String[] id = request.getParameterValues("typeId");
		for (int i = 0; i < id.length; i++) {
			albumType = tbAlbumTypeDao.findById(i);
			tbAlbumTypeDao.delete(albumType);
		}
		return SUCCESS;
	}
}
